//
//  ViewController.m
//  Alarm3DExperiments
//
//  Created by 우성 김 on 12. 3. 28..
//  Copyright (c) 2012년 yy01ar@gmail.com. All rights reserved.
//

#import "ViewController.h"
#import <MediaPlayer/MediaPlayer.h>
#import <AVFoundation/AVFoundation.h>

@interface ViewController ()

@end

@implementation ViewController
@synthesize volumeController;
@synthesize textfield;

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    
    // Add Volume View Experiments
//    volumeView = [[MPVolumeView alloc] initWithFrame:
//                                 CGRectMake(0, 0, 215, 22)];
//    volumeView.center = CGPointMake(150,375);
//    [volumeView sizeToFit];
//    [self.view addSubview:volumeView];
    
//    UISlider *slider ;
//    for(UIView * view in [volumeController subviews])
//    {
//            if([[[view class]description] isEqualToString:@"MPVolumeSlider"])
//            {
//                slider = view;
//            }
//    }
//    [slider setValue:.75f];
//    [slider _commitVolumeChange];
    volume = 0.5f;
    [[MPMusicPlayerController applicationMusicPlayer] setVolume:volume];
}

- (void)viewDidUnload
{
    [self setTextfield:nil];
    [self setVolumeController:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}
- (IBAction)volumeUp:(id)sender {
    volume += 0.05f;
    [[MPMusicPlayerController applicationMusicPlayer] setVolume:volume];
}

- (IBAction)volumeDown:(id)sender {
    volume -=0.05f;
    [[MPMusicPlayerController applicationMusicPlayer] setVolume:volume];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

- (IBAction)pushButton:(id)sender {
    UIButton *button = sender;

    [textfield setText:button.currentTitle];
}
- (IBAction)playAVMusic:(id)sender {
    
//    NSString *soundPath = [[NSBundle mainBundle] pathForResource:@"alarm_Disco_3s" ofType:@"m4a"];
//    NSLog(soundPath);
    NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/Ellen+McLain+-+Still+Alive.mp3", [[NSBundle mainBundle] resourcePath]]];
    NSError *error;
    
    audioPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:url error:&error];
    audioPlayer.volume = 1.0;
    if(audioPlayer == nil)
    {
        NSLog([error debugDescription]);
    }
    else
        [audioPlayer play];  
}

-(IBAction)openMediaPicker:(id)sender
{
    isMusic = YES;
    MPMediaPickerController *mediaPicker = [[MPMediaPickerController alloc] initWithMediaTypes:MPMediaTypeMusic];
    mediaPicker.delegate = self;
    mediaPicker.allowsPickingMultipleItems = NO; // this is the default   
    [self presentModalViewController:mediaPicker animated:YES];
    //    [mediaPicker release];
}

- (IBAction)pickAndMoviePlayer:(id)sender {
    isMusic = NO;
    MPMediaPickerController *mediaPicker = [[MPMediaPickerController alloc] initWithMediaTypes:MPMediaTypeMusic];
    mediaPicker.delegate = self;
    mediaPicker.allowsPickingMultipleItems = NO; // this is the default   
    [self presentModalViewController:mediaPicker animated:YES];
}

- (IBAction)mpPlayerAVList:(id)sender {
    NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/Ellen+McLain+-+Still+Alive.mp3", [[NSBundle mainBundle] resourcePath]]];
    
    MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
    [moviePlayer prepareToPlay];
    CGRect frame = CGRectMake(0, 0, 0, 0);
	[moviePlayer.view setFrame:frame];
    [moviePlayer setControlStyle:MPMovieControlStyleNone]; 
	[moviePlayer setRepeatMode:MPMovieRepeatModeOne];
    [self.view addSubview:moviePlayer.view];
    mpPlayer = moviePlayer;
//    [moviePlayer release];
    [mpPlayer play];
    
    
//    [[NSNotificationCenter defaultCenter] addObserver:self 
//											 selector:@selector(readyToPlay:) 
//												 name:MPMoviePlayerLoadStateDidChangeNotification 
//											   object:nil];
    
    
//    [moviePlayer prepareToPlay];
    
//    MPMusicPlayerController* appMusicPlayer = [MPMusicPlayerController applicationMusicPlayer];
//    MPMusicPlayerController* appMusicPlayer = [MPMusicPlayerController alloc] 
//    [appMusicPlayer ];
//    [appMusicPlayer play];
}

//- (void)readyToPlay:(NSNotification *)notification {
//	NSLog(@"readyToPlayIntro");
//	[self.mpPlayer play];
//	
//	[self performSelector:@selector(removeView) withObject:nil afterDelay:0.1];
//	
//}

// Media picker delegate methods
- (void)mediaPicker: (MPMediaPickerController *)mediaPicker didPickMediaItems:(MPMediaItemCollection *)mediaItemCollection {
    // We need to dismiss the picker
    [self dismissModalViewControllerAnimated:YES];
    
    for (MPMediaItem *item in mediaItemCollection.items)
    {
        NSLog([item valueForProperty:MPMediaItemPropertyTitle]);
//          NSLog([item valueForProperty:MPMediaItemPropertyAssetURL]);
    }
    MPMediaItem *item = [[mediaItemCollection items] objectAtIndex:0];
    NSURL *url = [item valueForProperty:MPMediaItemPropertyAssetURL];

    // AVAudioPlayer example
//    AVAudioPlayer *player = [[AVAudioPlayer alloc]initWithContentsOfURL:url error:nil];
//    player.volume = 1.0;
//    [player play];
    
    // MPMusicPlayer example
    if(isMusic) {
        MPMusicPlayerController* appMusicPlayer = [MPMusicPlayerController applicationMusicPlayer];
        [appMusicPlayer setQueueWithItemCollection:mediaItemCollection];
        [appMusicPlayer play];
    }
    else {
        MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
        [moviePlayer prepareToPlay];
        CGRect frame = CGRectMake(0, 0, 0, 0);
        [moviePlayer.view setFrame:frame];
        [moviePlayer setControlStyle:MPMovieControlStyleNone]; 
        [moviePlayer setRepeatMode:MPMovieRepeatModeOne];
        [self.view addSubview:moviePlayer.view];
        mpPlayer = moviePlayer;
    }
    

    // Assign the selected item(s) to the music player and start playback.
//    [self.musicPlayer stop];
//    [self.musicPlayer setQueueWithItemCollection:mediaItemCollection];
//    [self.musicPlayer play];
}

- (void)mediaPickerDidCancel:(MPMediaPickerController *)mediaPicker {
    // User did not select anything     
    // We need to dismiss the picker
    [self dismissModalViewControllerAnimated:YES];
}

- (IBAction)multiPlay:(id)sender {
    NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/Ellen+McLain+-+Still+Alive.mp3", [[NSBundle mainBundle] resourcePath]]];
    
    MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
    [moviePlayer prepareToPlay];
    CGRect frame = CGRectMake(0, 0, 0, 0);
	[moviePlayer.view setFrame:frame];
    [moviePlayer setControlStyle:MPMovieControlStyleNone]; 
	[moviePlayer setRepeatMode:MPMovieRepeatModeOne];
    [self.view addSubview:moviePlayer.view];
    mpPlayer = moviePlayer;
    //    [moviePlayer release];
    [mpPlayer play];
    
    NSURL *url2 = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/T.Park-normal(roof).mp4", [[NSBundle mainBundle] resourcePath]]];
    
    MPMoviePlayerController *moviePlayer2 = [[MPMoviePlayerController alloc] initWithContentURL:url2];
    [moviePlayer2 prepareToPlay];
    CGRect frame2 = CGRectMake(0, 0, 30, 30);
	[moviePlayer2.view setFrame:frame2];
    [moviePlayer2 setControlStyle:MPMovieControlStyleNone]; 
	[moviePlayer2 setRepeatMode:MPMovieRepeatModeOne];
    [self.view addSubview:moviePlayer2.view];
    mpPlayer2 = moviePlayer2;
    //    [moviePlayer release];
    [mpPlayer2 play];
    
}




@end
